import { Component, OnInit, ViewChild } from '@angular/core';
import { DatePipe } from '@angular/common';
import { NzMessageService } from 'ng-zorro-antd';
import { Router, NavigationEnd } from '@angular/router';
import { environment } from '../../../../environments/environment';
import { TableComponent } from './table.component';
import { ExportFileService } from '../../../core/exportFile';
import { LocalStorage } from '@core/local.storage';
import { FarmReactComponent } from '../../../layout/components/farmreact.component';

@Component({
    selector: 'app-dispatchlist',
    templateUrl: './dispatchList.component.html',
    styleUrls: ['./dispatchList.component.less']
})

export class DispatchListComponent implements OnInit {
    @ViewChild('tcp', { static: true }) tcp: TableComponent;
    @ViewChild('farmReact', { static: false }) farmReact: FarmReactComponent;
    searchParam: any = {};
    apiUrl: any = {};
    tableScroll: any = {};
    exportUrl = '';
    headData = [];
    plainData = [];
    tempData = [];
    operaList: any = [
        { addModal: false },
        { delModal: false },
        { uploadModal: false }
    ];
    workshopSelect = [];
    statusSelect = [];
    paramType = '';
    paramOver = '';
    tabIndex = 0;
    treeNodeStyle = { 'max-height': '300px', 'overflow-y': 'auto', 'overflow-x': 'hidden' };
    orgid = JSON.parse(this.ls.get('loginInfo')).VC_ORG$ID;
    level = JSON.parse(this.ls.get('loginInfo')).N_LEVEL;
    typeName = '';
    spztList = [
        { "opValue": "0", "opName": "工区编制" },
        { "opValue": "1", "opName": "工区提交" },
        { "opValue": "2", "opName": "车间同意" },
        { "opValue": "3", "opName": "车间驳回" },
        { "opValue": "4", "opName": "段同意" },
        { "opValue": "5", "opName": "段驳回" },
        { "opValue": "6", "opName": "反馈完成" }
    ];
    wcqkList = [
        { "opValue": "已反馈", "opName": "已反馈" },
        { "opValue": "未反馈", "opName": "未反馈" }
    ];

    constructor(
        private exportFile: ExportFileService,
        private msg: NzMessageService,
        private router: Router,
        public ls: LocalStorage,
        private datePipe: DatePipe
    ) {
        this.router.events.subscribe((event: any) => {
            if (event instanceof NavigationEnd) {
                if (event.url === '/dispatchList/workarea') {
                    this.loadList();
                }
            }
        });
    }

    ngOnInit() {
        this.headData = [
            { "name": "车间", "width": "8%" },
            { "name": "车站", "width": "8%" },
            { "name": "作业日期", "width": "8%" },
            { "name": "设备名称" },
            { "name": "维修分类", "width": "8%" },

            { "name": "天窗计划", "width": "12%" },
            { "name": "作业内容", "width": "12%" },
            { "name": "责任人", "width": "5%" },
            { "name": "问题描述", "width": "12%" },
            { "name": "审批状态", "width": "5%" },

            { "name": "完成情况", "width": "5%" }
        ];
        this.plainData = [
            "wsName", "staName", "dtWorkdate", "newDevname", "vcProjecttype",
            "vcSkyplanrange", "vcWorkinfo", "vcLeaderid", "vcProbdesc", "status", "vcFinished"
        ];
        this.apiUrl.add = environment.project + '/ymTDispatchlist/add';
        this.apiUrl.del = environment.project + '/ymTDispatchlist/delete';
        this.apiUrl.approve = environment.project + '/ymTDispatchlist/batchUpdate';
        this.apiUrl.upload = environment.project + '/ymTDispatchlist/batchUpdate';
        this.typeName = window.location.href.split('/')[window.location.href.split('/').length - 1];
        this.searchParam.dtWorkdate = new Date().getTime() + 24 * 60 * 60 * 1000;
        this.loadList();
    }
    loadList() {
        if (this.level === 10 || this.level === 30) {
            this.tcp.loadList(environment.project + '/ymTDispatchlist/query', '&vcOrgid=' + this.orgid);
        } else {
            this.tcp.loadList(environment.project + '/ymTDispatchlist/query', '');
        }
    }
    search() {
        let params = '';
        if (this.searchParam.dtWorkdate) {
            params += '&dtWorkdate=' + this.datePipe.transform(this.searchParam.dtWorkdate, 'yyyy-MM-dd');
        }
        if (this.searchParam.vcWorkinfo) {
            params += '&vcWorkinfo=' + this.searchParam.vcWorkinfo;
        }
        if (this.level === 10 || this.level === 30) {
            params += '&vcOrgid=' + this.orgid;
        }
        if (this.searchParam.vcStatus) {
            params += '&vcStatus=' + this.searchParam.vcStatus;
        }
        if (this.searchParam.vcFinished) {
            params += '&vcFinished=' + this.searchParam.vcFinished;
        }
        this.farmReact.childOuter((res) => { 
            if (res.chejianValue !== '0') {
                params += '&workshopId=' + (res.chejianValue || '');
            }
            if (res.gongquValue !== '0') {
                params += '&workAreaId=' + (res.gongquValue || '');
            }
            if (res.chezhanValue !== '0') {
                params += '&staId=' + (res.chezhanValue || '');
            }
        });
        this.tcp.loadList(environment.project + '/ymTDispatchlist/query', params);
    }
    export() {
        let status = false;
        let count = 0;
        const vcId = [];
        for (const item of this.tcp.getAllData()) {
            if (item.checked) {
                count += 1;
                vcId.push(item.vcId);
                status = true;
            }
        }
        if (status) {
            this.exportFile.postDown(environment.project + '/ymTDispatchlist/exportBatch', { vcIdList: vcId }, '作业工单导出.xls');
        } else {
            this.msg.error('请选择一条数据进行操作');
        }
    }
    feedback() {
        let status = false;
        let count = 0;
        let objData = {};
        for (const item of this.tcp.getAllData()) {
            if (item.checked) {
                count += 1;
                objData = item;
                status = true;
            }
        }
        if (status && count === 1) {
            this.router.navigate(['dispatchList/executionDetail'], {
                queryParams: objData
            });
        } else {
            this.msg.error('请选择一条数据进行操作');
        }
    }
    modalShow(type) {
        this.tempData = this.tcp.getAllData();
        this.operaList[type] = true;
    }
    modalHide(type) {
        this.operaList[type] = false;
    }
}
